package com.sj.uabrowser.mvp.menu.view.act;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.sj.uabrowser.R;
import com.sj.uabrowser.app.StatusBarUtil;
import com.sj.uabrowser.db.CollectHistory;
import com.sj.uabrowser.db.gen.CollectHistoryDao;
import com.sj.uabrowser.framework.BaseActivity;
import com.sj.uabrowser.framework.BasePresenter;
import com.sj.uabrowser.mvp.main.view.act.BrowserAct;
import com.sj.uabrowser.mvp.menu.view.adp.HistoryAdp;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

public class HistoryAct extends BaseActivity {

    @BindView(R.id.rvHistory)
    RecyclerView rvHistory;

    @BindView(R.id.ll_bottom_edit)
    LinearLayout llBottomEdit;
    @BindView(R.id.tvEdit)
    TextView tvEdit;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.multipleStatusView)
    MultipleStatusView multipleStatusView;

    private CollectHistoryDao collectHistoryDao;
    private HistoryAdp historyAdp;
    List<CollectHistory> datas = new ArrayList<>();

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_history;
    }

    @Override
    public void setView() {
        StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        StatusBarUtil.setLightMode(this);
        setTopTitle("历史记录");
        collectHistoryDao = daoSession.getCollectHistoryDao();
        historyAdp = new HistoryAdp(R.layout.common_list_item_history, datas);
        rvHistory.setLayoutManager(new LinearLayoutManager(this));
        rvHistory.setAdapter(historyAdp);
        historyAdp.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CollectHistory bean = datas.get(position);
                if (!isEdit) {
                    int type = bean.getType();
                    Intent intent = new Intent();
                    if (type == CollectHistory.TYPE_NEWS || type == CollectHistory.TYPE_KEYWORDS || type == CollectHistory.TYPE_NET) {
                        intent.setClass(HistoryAct.this, BrowserAct.class);
                        intent.putExtra("bean", bean);
                        intent.putExtra("type", BrowserAct.TYPE_BEAN);
                        startActivity(intent);
                    } else if (type == CollectHistory.TYPE_VIDEO) {

                    }
                } else {
                    bean.setChecked(!bean.isChecked());
                    CheckBox cb = view.findViewById(R.id.cb);
                    cb.setChecked(bean.isChecked());
                }
            }
        });

        historyAdp.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                RxDialogSureCancel rxDialogSureCancel = new RxDialogSureCancel(HistoryAct.this);
                rxDialogSureCancel.getTitleView().setVisibility(View.GONE);
                rxDialogSureCancel.setContent("确定要删除该条记录吗？");
                rxDialogSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CollectHistory bean = datas.get(position);
                        if(bean.getIsLike()){
                            bean.setIsHis(false);
                            collectHistoryDao.update(bean);
                        }else{
                            collectHistoryDao.delete(bean);
                        }
                        rxDialogSureCancel.dismiss();
                    }
                });
                rxDialogSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel.dismiss();
                    }
                });
                rxDialogSureCancel.show();
                return true;
            }
        });
    }

    private void onBackMain() {
        View tvBackMain = multipleStatusView.findViewById(R.id.tv_back_main);
        if(tvBackMain!=null) tvBackMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startIconAnim(v);
                finish();
            }
        });
    }

    @Override
    public void setData() {
        datas.addAll(collectHistoryDao.queryRaw("where is_his = ? order by date desc", new String[]{1 + ""}));
        Set<String> parentDatas = new HashSet<>();
        if (datas.size() > 0) {
            tvEdit.setText("编辑");
            for (int i = 0; i < datas.size(); i++) {
                CollectHistory bean = datas.get(i);
                long date = bean.getDate();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = sdf.format(date);
                if (!parentDatas.contains(time)) {
                    bean.setHeader(true);
                    bean.setTime(time);
                    parentDatas.add(time);
                }else{
                    bean.setTime("");
                }
            }
            historyAdp.notifyDataSetChanged();
        }else{
            multipleStatusView.showEmpty();
            onBackMain();
        }
    }

    boolean isEdit = false;

    @OnClick({R.id.tvEdit, R.id.tv_confirm, R.id.tv_delete})
    public void onEdit(View view) {
        startIconAnim(view);
        switch (view.getId()) {
            case R.id.tvEdit:
                isEdit = !isEdit;
                historyAdp.setEdit(isEdit);
                if (!isEdit) setCancelStatus();
                historyAdp.notifyDataSetChanged();
                tvEdit.setText(isEdit ? "取消" : "编辑");
                llBottomEdit.setVisibility(isEdit ? View.VISIBLE : View.GONE);

                break;
            case R.id.tv_confirm://全选
                isSelectedAll = !isSelectedAll;
                tvConfirm.setText(isSelectedAll ? "不选" : "全选");
                for (int i = 0; i < datas.size(); i++) {
                    datas.get(i).setChecked(isSelectedAll);
                }
                historyAdp.notifyDataSetChanged();
                break;
            case R.id.tv_delete://删除
                int count = 0;
                for (int i = 0; i < datas.size(); i++) {
                    if(datas.get(i).isChecked())count++;
                }
                if(count==0){
                    showToast("没有选择项，无法删除！");
                    return;
                }
                RxDialogSureCancel rxDialogSureCancel = new RxDialogSureCancel(this);
                rxDialogSureCancel.getTitleView().setVisibility(View.GONE);
                rxDialogSureCancel.setContent("确定要删除"+count+"项吗？");
                rxDialogSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteDatas();
                        rxDialogSureCancel.dismiss();
                    }
                });
                rxDialogSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel.dismiss();
                    }
                });
                rxDialogSureCancel.show();
                break;
        }

    }

    private void deleteDatas() {
        List<CollectHistory> removeDatas = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            CollectHistory bean = datas.get(i);
            if (bean.isChecked()) {
                removeDatas.add(bean);
            }
        }
        datas.removeAll(removeDatas);
        for (int i = 0; i < removeDatas.size(); i++) {
            CollectHistory bean = removeDatas.get(i);
            if (bean.isChecked()) {
                boolean isLike = bean.getIsLike();
                if (isLike) {
                    bean.setIsHis(false);
                    collectHistoryDao.update(bean);
                } else {
                    collectHistoryDao.delete(bean);
                }
            }
        }
        setCancelStatus();
    }

    private boolean isSelectedAll = false;

    private void setCancelStatus() {
        for (int i = 0; i < datas.size(); i++) {
            datas.get(i).setChecked(false);
        }
        isEdit = false;
        historyAdp.setEdit(isEdit);
        historyAdp.notifyDataSetChanged();
        llBottomEdit.setVisibility(View.GONE);
        tvEdit.setText(datas.size() > 0 ? "编辑" : "");
        if(datas.size()==0){
            multipleStatusView.showEmpty();
            onBackMain();
        }
    }


}
